#!/bin/bash

# 热点追踪系统监控脚本
echo "📊 热点追踪系统监控面板"
echo "================================"

# 检查容器状态
echo "🐳 容器状态:"
docker-compose ps

echo ""
echo "📈 资源使用情况:"
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"

echo ""
echo "🏥 健康检查:"

# Redis健康检查
redis_health=$(docker-compose exec -T redis redis-cli ping 2>/dev/null)
echo "Redis: ${redis_health:-FAILED}"

# API健康检查
api_health=$(curl -s http://localhost:3001/api/health 2>/dev/null | jq -r '.status' 2>/dev/null || echo "FAILED")
echo "API: ${api_health}"

# Frontend健康检查
frontend_health=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/ 2>/dev/null)
if [ "$frontend_health" = "200" ]; then
    echo "Frontend: OK"
else
    echo "Frontend: FAILED (HTTP $frontend_health)"
fi

echo ""
echo "📊 API统计:"
# 获取热点数据统计
hotspots_count=$(curl -s http://localhost:3001/api/hotspots 2>/dev/null | jq '.data | length' 2>/dev/null || echo "N/A")
echo "当前热点数量: $hotspots_count"

echo ""
echo "💾 磁盘使用:"
docker system df

echo ""
echo "📝 最近日志 (最后10行):"
echo "--- API 日志 ---"
docker-compose logs --tail=5 api

echo ""
echo "--- Nginx 日志 ---"
docker-compose logs --tail=5 frontend